*******************************************************************************
* > Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_gss, clear
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

******************************************************************************
* Combine items with alt wordings
******************************************************************************

replace target = "aid" if target == "aidy"
replace target = "arms" if target == "armsy"
replace target = "city" if target == "cityy"
//replace target = "crime" if target == "crimy"
replace target = "drug" if target == "drugy"
replace target = "educ" if target == "educy"
replace target = "envir" if target == "enviy"
replace target = "fare" if target == "farey"
replace target = "heal" if target == "healy"
replace target = "race" if target == "racey"
replace target = "spac" if target == "spacy"

collapse nat, by(year target)

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local mylabel "Law Enforcement"

local legend1 legend(off)
if "`subgroup'" == "All" {
	local legend1 legend(order(1 ///
		"{fontface monospace:{bf:`mylabel'}}" ///
		2 "{fontface monospace:Control Series}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)) fcol(none))	
}

*******************************************************************************
* Set params based on data set
*******************************************************************************

rename nat Y
local treated_target "crimy"
local yaxis -.6(.2).6
local xaxis 1982(8)2022	

*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* Loop over placebo series 
*******************************************************************************

encode target, gen(id)
tsset id year
local i = 1
levelsof target, local(levels)
local nlevels = r(r)
foreach l of local levels {
	
	su id if target == "`l'"
	local treated = r(mean)

*******************************************************************************
* Do the synth
*******************************************************************************

	#delimit;

	synth 
		Y `lags',
			trunit(`treated')
			trperiod(2014)   
			keep(tmp_`i', replace)
			;

	#delimit cr
		
	preserve
		use tmp_`i', clear
		gen target = "`l'"
		save tmp_`i', replace
	restore

	local i = `i' + 1
		
}

*******************************************************************************
* Graph all placebo series
*******************************************************************************

use tmp_1, clear
erase tmp_1.dta
forvalues i = 2(1)`nlevels' {
	append using tmp_`i'
	erase tmp_`i'.dta
}

gen D = _Y_treat - _Y_syn
sort target _time
encode target, gen(targetid)

levelsof targetid, local(levels)
foreach l of local levels {
	local p (line D _time if targetid == `l', col(gray))
	local placebos `placebos' `p'
}

#delimit;

gr tw
	`placebos'
	(line D _time if target == "`treated_target'", col(black) lwid(thick))
	,
		`legend1'
		xline(2014, lpat(dash))
		yline(0, lpat(shortdash))
		name(g1, replace)
		plotregion(style(none))
		ylab(`yaxis', angle(horiz))
		ytitle("")
		xtitle("")
		xlab(`xaxis')
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

*******************************************************************************
* Graph RMSPE (one-sided test)
*******************************************************************************

gen DD = D
if "`subgroup'" != "Republican" replace DD = 0 if DD > 0 & _time >= 2014
if "`subgroup'" == "Republican" replace DD = 0 if DD < 0 & _time >= 2014
gen pre_mspem = DD^2 if _time < 2014
egen pre_mspe = mean(pre_mspem), by(target)
gen post_mspem = DD^2 if _time >= 2014
egen post_mspe = mean(post_mspem), by(target)

gen ratio_mspe = sqrt(post_mspe) / sqrt(pre_mspe)

collapse ratio, by(target)
sort ratio
gen index = _n

replace target = "Foreign aid" if target == "aid"
replace target = "Foreign aid alt" if target == "aidy"
replace target = "Military" if target == "arms"
replace target = "Military alt" if target == "armsy"
replace target = "Cities" if target == "city"
replace target = "Cities alt" if target == "cityy"
replace target = "Crime" if target == "crime"
replace target = "Law enforcement" if target == "crimy"
replace target = "Drugs" if target == "drug"
replace target = "Drugs alt" if target == "drugy"
replace target = "Education" if target == "educ"
replace target = "Education alt" if target == "educy"
replace target = "Environment" if target == "envir"
replace target = "Environment alt" if target == "enviy"
replace target = "Welfare" if target == "fare"
replace target = "Welfare alt" if target == "farey"
replace target = "Health" if target == "heal"
replace target = "Health alt" if target == "healy"
replace target = "Transportation" if target == "mass"
replace target = "Parks and rec" if target == "park"
replace target = "Blacks" if target == "race"
replace target = "Blacks alt" if target == "racey"
replace target = "Roads" if target == "road"
replace target = "Social Security" if target == "soc"
replace target = "Space" if target == "spac"
replace target = "Space alt" if target == "spacy"

replace target = target + (15-length(target))*" "
replace target = "{fontface monospace:" + target + "}"

labmask index, values(target)

#delimit;

gr tw
	(sc index ratio if regex(target, "Law enforcement") | 
		regex(target, "Police"), 
		col(black) msym(O))
	(sc index ratio if !regex(target, "Law enforcement") & 
		!regex(target, "Police"), 
		col(black) msym(Oh))
	, 
		name(g2, replace)
		ylab(1(1)`nlevels', angle(horiz) valuelabel labsize(vsmall))
		plotregion(style(none))
		ytitle("")
		xtitle("Postperiod RMSPE / Preperiod RMSPE", size(small))
		legend(off)
		;
		
#delimit cr

*******************************************************************************
* Combine plots 
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* > Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each subgroup
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_group_gss.pdf", replace

*******************************************************************************
* End
*******************************************************************************
